package com.itouch.bracelet.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.itouch.bracelet.R;
import com.itouch.bracelet.adapter.MMessageAdapter;
import com.itouch.bracelet.bean.LoginUser;
import com.itouch.bracelet.bean.MMessage;
import com.itouch.bracelet.bean.MMessageResult;
import com.itouch.bracelet.cache.UserCache;
import com.itouch.bracelet.diseaseinfo.Constants;
import com.itouch.widget.XListView;
import com.itouch.widget.XListView.IXListViewListener;

public class MMessageListActivity extends BaseActivity implements
		IXListViewListener {

	private XListView listview;
	private ArrayList<MMessage> list;
	private MMessageAdapter adapter;
	private int page_index = 1;
	private static final int page_size = 10;
//	private int type;

	@Override
	protected int setContentViewById() {
		return R.layout.activity_mmessage_list;
	}


	@Override
	protected void initViews() {

		listview = (XListView) findViewById(R.id.listview);
		listview.setPullRefreshEnable(true);
		listview.setPullLoadEnable(false);
		listview.setXListViewListener(this);
		listview.setRefreshTime(getTime());
		// listview.setOnItemClickListener(this);
		list = new ArrayList<MMessage>();
		adapter = new MMessageAdapter(context, list);
		listview.setAdapter(adapter);

		getList();
	}

	private void getList() {
		LoginUser user=UserCache.getOauth(context);
		if(user==null){
			return;
		}
		
		Uri.Builder builder = Uri.parse(Constants.GET_RONG_MSG).buildUpon();
		
		builder.appendQueryParameter("page", page_index + "");
		builder.appendQueryParameter("rows", page_size + "");
		builder.appendQueryParameter("filter_S_receiver", user.getUserId()+ "");

		if (page_index == 1) {
			list.clear();
		}
		Log.d("GET_RONG_MSG", "GET_RONG_MSG" + builder.toString());

		show();
		getJSONByVolleyGET(builder.toString(),
				new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject arg0) {
						disMiss();
						onLoad();
						Log.d("GET_RONG_MSG",
								"GET_RONG_MSG" + arg0.toString());
						try {
						MMessageResult a = new Gson().fromJson(
									arg0.toString(), MMessageResult.class);
							if (a == null) {
								return;
							}
							if (a.isFlag()) {
								List<MMessage> xx = a.getRows();
								if (xx != null) {
									list.addAll(xx);
									adapter.notifyDataSetChanged();
									if (xx.size() < page_size) {
										setPullLoadEnable(false);

									} else {
										setPullLoadEnable(true);

									}
								}

							} else {
								Toast.makeText(context, a.getMsg() + "",
										Toast.LENGTH_LONG).show();
							}
						} catch (Exception e) {
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError arg0) {
						disMiss();
						onLoad();
						Toast.makeText(context,
								getString(R.string.net_error) + "",
								Toast.LENGTH_LONG).show();
					}
				});

	}

	@Override
	public void onRefresh() {
		page_index = 1;
		getList();
	}

	@Override
	public void onLoadMore() {
		page_index++;
		getList();
	}

	private void onLoad() {
		if (null != listview) {
			listview.stopRefresh();
			listview.stopLoadMore();
			listview.setRefreshTime(getTime());
		}
	}

	private void setPullLoadEnable(boolean temp) {
		if (listview != null) {
			listview.setPullLoadEnable(temp);
		}
	}

	@Override
	protected void initData() {

	}

}
